<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>四则运算计算器</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            margin: 0;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
        }

        .calculator {
            background: white;
            padding: 2rem;
            border-radius: 15px;
            box-shadow: 0 10px 20px rgba(0,0,0,0.1);
            width: 350px;
        }

        .input-group {
            margin-bottom: 1.5rem;
        }

        input {
            width: 100%;
            padding: 0.8rem;
            margin: 0.5rem 0;
            border: 2px solid #e0e0e0;
            border-radius: 8px;
            font-size: 16px;
            transition: border-color 0.3s ease;
        }

        input:focus {
            outline: none;
            border-color: #2196F3;
        }

        .buttons {
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            gap: 0.5rem;
            margin-bottom: 1.5rem;
        }

        button {
            padding: 0.8rem;
            border: none;
            border-radius: 8px;
            background: #2196F3;
            color: white;
            font-size: 16px;
            cursor: pointer;
            transition: all 0.2s ease;
        }

        button:hover {
            background: #1976D2;
            transform: translateY(-2px);
        }

        button:active {
            transform: translateY(0);
        }

        #result {
            padding: 1rem;
            background: #f8f9fa;
            border-radius: 8px;
            text-align: center;
            font-size: 18px;
            min-height: 60px;
        }

        .error {
            color: #dc3545;
            background: #ffeef0;
            border: 1px solid #ffd1d1;
        }

        .success {
            color: #28a745;
            background: #e8f5e9;
            border: 1px solid #c8e6c9;
        }
    </style>
</head>
<body>
    <div class="calculator">
        <h2 style="text-align: center; margin-bottom: 1.5rem; color: #2c3e50;">简易计算器</h2>
        
        <div class="input-group">
            <input type="number" id="num1" placeholder="输入第一个数字" step="any">
            <input type="number" id="num2" placeholder="输入第二个数字" step="any">
        </div>

        <div class="buttons">
            <button onclick="calculate('+')">＋</button>
            <button onclick="calculate('-')">－</button>
            <button onclick="calculate('×')">×</button>
            <button onclick="calculate('÷')">÷</button>
        </div>

        <div id="result"></div>
    </div>

    <script>
        function calculate(operator) {
            const num1 = parseFloat(document.getElementById('num1').value);
            const num2 = parseFloat(document.getElementById('num2').value);
            const resultDiv = document.getElementById('result');
            
            // 清除之前的样式
            resultDiv.className = '';
            
            // 验证输入
            if (isNaN(num1) || isNaN(num2)) {
                resultDiv.textContent = "请输入有效的数字！";
                resultDiv.classList.add('error');
                return;
            }

            let result;
            switch(operator) {
                case '+':
                    result = num1 + num2;
                    break;
                case '-':
                    result = num1 - num2;
                    break;
                case '×':
                    result = num1 * num2;
                    break;
                case '÷':
                    if (num2 === 0) {
                        resultDiv.textContent = "除数不能为零！";
                        resultDiv.classList.add('error');
                        return;
                    }
                    result = num1 / num2;
                    break;
            }

            // 显示结果（保留最多4位小数）
            resultDiv.innerHTML = `
                <div>${num1} ${operator} ${num2} =</div>
                <div style="font-size: 24px; margin-top: 8px;">${parseFloat(result.toFixed(4))}</div>
            `;
            resultDiv.classList.add('success');
        }
    </script>
</body>
</html>